package com.offcn.dao;

import com.offcn.entity.User;
import com.offcn.utils.BaseDao;

import java.util.Date;
import java.util.List;
/**
 * 用户Dao层实现类
 * 继承了BaseDao工具类 其内封装了通用的增删改查方法
 * 实现了用户Dao接口
 */
public class UserDaoImpl extends BaseDao<User> implements UserDao {

    @Override
    public User login(String username, String password) {
        return getOne("select * from user where username=? and password=?",User.class,username,password);
    }


    @Override
    public int addUser(User user) {
        return update("insert into user values(null,?,?,?,?,?,?,?,now(),?,null)",
                user.getName(),user.getPhone(),user.getAge(),user.getSex(),user.getUsername()
                ,user.getPassword(),user.getStatus(),user.getRole());
    }


    @Override
    public List<User> findByPage(String sql, Object... params) {
        return getMore(sql,User.class,params);
    }

    @Override
    public int updateUserById(User user) {
        String sql = "update user set name =?,phone=?,age=?,sex=?,username=?,password=?,status=?,createTime=?,role=?,picture=? where uid =?";
        return update(sql,user.getName(), user.getPhone(), user.getAge(), user.getSex(), user.getUsername(), user.getPassword(), user.getStatus(), new Date(),
                user.getRole(), user.getPicture(),user.getUid());
    }

    @Override
    public int delAll(String uid) {
        return update("delete from user where uid=?",uid);
    }

    @Override
    public User findById(int uid) {
        return getOne("select * from user where uid=?",User.class,uid);
    }


}
